<?php
class My_API_sm07_Users
{
    private $_config;

    public function __construct($gameId = null)
    {
        if(!isset($gameId) || !file_exists(CONFIG_PATH2 . "/$gameId.ini")){
            return false;
        }
        
        $config = new Zend_Config_Ini(CONFIG_PATH2 . "/$gameId.ini",APPLICATION_ENV);
        $this->_config = $config;
    }
    
    public function getUsersByServerId($listUser, $serverId, $channel = ''){
        $i = 0;
		$infoUsers = array();
        $serverIdPartner = str_replace('S','',$serverId);
        
		$userCheck = array_chunk($listUser, 50);
		foreach ($userCheck as $listCheck){				
			//get qid of listuser	
            $accounts = implode(',',$listCheck);
            $urlAPI = "{$this->_config->getUserInfo->url}?User=$accounts&Server_id=1000{$serverIdPartner}";
            //echo $urlAPI."<br />";//exit;
            $result = file_get_contents($urlAPI);
            $result = json_decode($result);
            
            if(isset($result->status) && $result->status == 1){
                foreach($result->result as $key=>$row){
                    if(!isset($row)){
                        continue;
                    }
                    
					$infoUsers[$i]['serverId'] = $serverId;
					$infoUsers[$i]['roleName'] = $row->roleName;
					$infoUsers[$i]['roleLevel'] = $row->level;
                    if(isset($row->createStamp)){
                        $infoUsers[$i]['roleCreate'] = $row->createStamp;
                    }
					$i++;
                }
			}else{
				continue;
			}
		}
        
		return $infoUsers;
    }
    
    public function getUsernameByRolename($listRoleName, $serverId){
        $i = 0;
		$infoUsername = array();
        $serverIdPartner = str_replace('S','',$serverId);
        
		$userCheck = array_chunk($listRoleName, 50);
		foreach ($userCheck as $listCheck){
			//get qid of listuser	
            $accounts = implode(',',$listCheck);
            $urlAPI = "{$this->_config->getUsernameInfo->url}?User=$accounts&Server_id=1000{$serverIdPartner}";
            //echo $urlAPI."<br />";exit;
            $result = file_get_contents($urlAPI);
            $result = json_decode($result);
            
            if(isset($result->status) && $result->status == 1){
                foreach($result->result as $row){
					$infoUsername[$i]['serverId'] = $serverId;
					$infoUsername[$i]['username'] = $row->username;
					$infoUsername[$i]['roleLevel'] = $row->level;
					$i++;
                }
			}else{
				continue;
			}
		}
        
		return $infoUsername;
    }
}